//
//  MainViewAnimationModel.m
//  Tencent
//
//  Created by admin on 15/5/17.
//  Copyright (c) 2015年 Leo. All rights reserved.
//

#import "KKSideViewController.h"

#import <objc/runtime.h>

static NSTimeInterval AnimationDuration = 0.15;
static CGFloat AnimationInformationViewCornerRadius = 25.0f;

static NSString *KKSideViewControllerNotificationName = @"KKSideViewControllerNotificationName";

@interface KKSideViewController() <UIGestureRecognizerDelegate> {
    
    //  InformationViewController
    UIView* _animationView;
    UIView* _animationEventView;
    
    CGFloat _maxOffsetX;
    CGPoint _resetCenter;
    
    UIView* _contentBackgroundView;
    
    UIPanGestureRecognizer *_panner;
}

@end

@implementation KKSideViewController

- (void)loadView {
    [super loadView];
    
    _contentBackgroundView = [[UIView alloc] initWithFrame: self.view.bounds];
    _contentBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [self.view addSubview: _contentBackgroundView];
    
    _contentView = [[UIView alloc] initWithFrame: self.view.bounds];
    _contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [self.view addSubview: _contentView];
    
 
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(startAnimating) name:KKSideViewControllerNotificationName object: nil];
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver: self];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    
    return true;
}

#pragma mark - Public methods

- (void)addChildViewController:(UIViewController *)childController Sideslip:(BOOL)sideslip {
    
    [super addChildViewController: childController];
    //  添加滑动事件操作
    _panner = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @selector(onPan:)];
    _panner.delegate = self;
    
    CGFloat width = CGRectGetWidth(self.view.bounds);
    CGFloat height = CGRectGetHeight(self.view.bounds);
    _resetCenter = CGPointMake(width/2, height/2);
    _maxOffsetX = width * 3 / 5;
    
    _contentView.center = CGPointMake(_resetCenter.x - _maxOffsetX, _resetCenter.y);
    _contentView.transform = CGAffineTransformMakeScale(0.8, 0.8);
    //
    _animationView = childController.view;
    _animationView.clipsToBounds = YES;
    _animationView.layer.masksToBounds = YES;
    
    _animationEventView = [[UIView alloc] initWithFrame: _animationView.bounds];
    _animationEventView.hidden = true;
    [_animationView addSubview: _animationEventView];
    [_animationEventView addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(onTap:)]];
}

#pragma mark - Public methods
- (void)startAnimating {
    
    _animationEventView.hidden = NO;
    __block UIView* blockInformationEventView = _animationEventView;
    __block UIView* blockInformationView = _animationView;
    
    __block UIView* blockMainPannelView = _contentView;
    __block UIView* blockSelfView = _contentBackgroundView;
    [UIView animateWithDuration: AnimationDuration animations:^{
        [blockInformationView setCenter: CGPointMake(_resetCenter.x + _maxOffsetX, _resetCenter.y)];
        blockInformationView.transform = CGAffineTransformMakeScale(0.8, 0.8);
        blockInformationView.layer.cornerRadius = AnimationInformationViewCornerRadius;
        
        blockMainPannelView.center = CGPointMake(_resetCenter.x , _resetCenter.y);
        blockMainPannelView.transform = CGAffineTransformIdentity;
        blockSelfView.backgroundColor = [UIColor colorWithWhite: 0.1 alpha: 0.3];
        
    }completion:^(BOOL finished) {
        blockInformationEventView.hidden = NO;
        
        [self.view addGestureRecognizer: _panner];
    }];
}

- (void)reset {
    
    __block UIView* blockInformationEventView = _animationEventView;
    __block UIView* blockInformationView = _animationView;
    
    __block UIView* blockMainPannelView = _contentView;
    __block UIView* blockSelfView = _contentBackgroundView;
    
    [UIView animateWithDuration: AnimationDuration animations:^{
        
        blockInformationView.center = _resetCenter;
        blockInformationView.transform = CGAffineTransformIdentity;
        blockInformationView.layer.cornerRadius = 0.0f;
        
        blockMainPannelView.center = CGPointMake(_resetCenter.x - _maxOffsetX, _resetCenter.y);
        blockMainPannelView.transform = CGAffineTransformMakeScale(0.8, 0.8);
        blockSelfView.backgroundColor = [UIColor colorWithWhite: 0.1 alpha: 1.0];
    }completion:^(BOOL finished) {
        blockInformationEventView.hidden = YES;
        
        [self.view removeGestureRecognizer: _panner];
    }];
    
}

#pragma mark - UIPanGestureRecognizer methods

- (void)onTap:(UITapGestureRecognizer*)tapper {
    
    UIView* target = [tapper view];
    if (target == _animationEventView) {
        
        [self reset];
        return;
    }
}

static CGPoint BeganPoint;
- (void)onPan:(UIPanGestureRecognizer*)panner {
    
    CGPoint point = [panner translationInView: self.view];
    
    UIGestureRecognizerState state = panner.state;
    switch (state) {
        case UIGestureRecognizerStateBegan:
        {
            BeganPoint = point;
            
            if (CGPointEqualToPoint(_animationView.center, _resetCenter)) {
                _contentView.center = CGPointMake(_resetCenter.x - _maxOffsetX, _resetCenter.y);
            }
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            CGFloat offsetX = point.x - BeganPoint.x;
            CGPoint informationCenter = _animationView.center;
            informationCenter.x += offsetX;
            
            CGPoint mainPannelCenter = _contentView.center;
            mainPannelCenter.x += offsetX;
            
            if (informationCenter.x >= _resetCenter.x && informationCenter.x <= _resetCenter.x + _maxOffsetX) {
                
                //偏移的比例
                CGFloat proportion = (informationCenter.x - _resetCenter.x) / _maxOffsetX;
                
                CGFloat offsetScale = proportion * 0.2;
                CGFloat radius = proportion * AnimationInformationViewCornerRadius;
                
                _animationView.center = informationCenter;
                _animationView.transform = CGAffineTransformMakeScale(1.0 - offsetScale, 1.0 - offsetScale);
                _animationView.layer.cornerRadius = radius;
                
                _contentView.center = mainPannelCenter;
                _contentView.transform = CGAffineTransformMakeScale(0.8 + offsetScale, 0.8 + offsetScale);
                
                CGFloat alpha = 1.3 - proportion;
                _contentBackgroundView.backgroundColor = [UIColor colorWithWhite: 0.1 alpha: alpha];
            }
            
            BeganPoint = point;
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            CGPoint informationCenter = _animationView.center;
            
            if (informationCenter.x < _resetCenter.x + CGRectGetWidth(_animationView.bounds)/4) {
                [self reset];
            }
            else {
                [self startAnimating];
            }
        }
            break;
            
        default:
            break;
    }
}

@end



@implementation UIViewController(KKSide)

@dynamic showMainItem;

static const void *UIViewControllerShowMainItemButtonValue = &UIViewControllerShowMainItemButtonValue;

- (BOOL)showMainItem {
    
    return [objc_getAssociatedObject(self, UIViewControllerShowMainItemButtonValue) boolValue];
}

- (void)setShowMainItem:(BOOL)showMainItem {
    
    BOOL value = [self showMainItem];
    
    if (value && showMainItem) {
        return;
    }
    else {
        if (value && !showMainItem) {
            self.navigationItem.leftBarButtonItem = nil;
        }
        else {
            UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage: [UIImage imageNamed: @"navigationItem_left"] style: UIBarButtonItemStylePlain target: self action: @selector(sideOfOclickFromLeftItem)];
            leftItem.tintColor = [UIColor whiteColor];
//        UIBarButtonItem *leftSpaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target: nil action: nil];
//        leftSpaceItem.width = -10;
//        self.navigationItem.leftBarButtonItems = @[leftSpaceItem, leftItem];
            self.navigationItem.leftBarButtonItem = leftItem;
        }
    }
    
    objc_setAssociatedObject(self, UIViewControllerShowMainItemButtonValue, [NSNumber numberWithBool: showMainItem], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma mark - Private methods

- (void)sideOfOclickFromLeftItem {
    
    [[NSNotificationCenter defaultCenter] postNotificationName: KKSideViewControllerNotificationName object: nil];
}

@end




